﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 资料试题控制器
    /// </summary>
    public class TrainFileQuestionController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(int AutoID)
        {
            Model.TrainFileQuestion memp = new Model.TrainFileQuestion();
            memp = new BLL.TrainFileQuestion().GetModel(AutoID);
            return JsonConvert.SerializeObject(memp);
        }

        /// <summary>
        /// 文件试题明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainFileQuestion/GetTrainFileQuestionDetail")]
        public IHttpActionResult GetTrainFileQuestionDetail(long TFID)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                DataTable dt = new BLL.TrainFileQuestion().GetList(" IsUse=1 and TFID="+ TFID.ToString()).Tables[0];
                resultMsg.Info = dt.Rows.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
                resultMsg.Count = dt.Rows.Count;
                resultMsg.Data = dt;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }


        /// <summary>
        /// 文件试题明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainFileQuestion/GetSingleTrainFileQuestion")]
        public IHttpActionResult GetSingleTrainFileQuestion(long TFQID)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                DataTable dt = new BLL.TrainFileQuestion().GetList(" IsUse=1 and TFQID=" + TFQID.ToString()).Tables[0];
                resultMsg.Info = dt.Rows.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
                resultMsg.Count = dt.Rows.Count;
                resultMsg.Data = dt;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 试题增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainFileQuestion/Add")]
        public IHttpActionResult Add(Model.TrainFileQuestion model)
        {
            log.Info(JsonConvert.SerializeObject(model));
            
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                BLL.TrainFileQuestion bTpq = new BLL.TrainFileQuestion();
                if (!bTpq.Exists(model.Question))
                {
                    model.TFQID = new BLL.SysManage().GetMaxId("TrainFileQuestion", "TFQID");
                    model.IsUse = 1;
                    if (new IPR_StudyMeetingMrgSys.BLL.TrainFileQuestion().Add(model)>0)
                    {
                        resultMsg.Info = "增加成功";
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "增加失败";
                    }
                }
                else
                {
                    resultMsg.Info = "试题信息已经存在，请不要重复增加";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        [HttpGet]
        [Route("api/TrainFileQuestion/AddList")]
        public IHttpActionResult AddList(string TFID,string TQIDList)
        { 

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                if( new IPR_StudyMeetingMrgSys.BLL.TrainFileQuestion().AddList(TFID, TQIDList)>0) 
                {
                    
                        resultMsg.Info = "增加成功";
                        resultMsg.Count = 1; 
                }
                else
                {
                    resultMsg.Info = "请选择要增加的试题";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        // POST: api/T_UserInfo
        /// <summary>
        /// 人员信息分页查找
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainFileQuestion/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = string.Empty;
                    swhere = " IsUse=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "TFID")
                        {
                            swhere += $" and TFID = '{item.Value}'";
                        } 
                    }
                }
                int rowCount = 0;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainFileQuestion().GetListByPage(swhere, "AutoID", startindex, endindex, out rowCount);
                resultMsg.Info = rowCount > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
                resultMsg.Count = rowCount;
                resultMsg.Data = dsSearch.Tables[0];
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
         
         
        [HttpGet]
        [Route("api/TrainFileQuestion/DelList")]
        public IHttpActionResult DelList(string delidlist)
        {
            log.Info(JsonConvert.SerializeObject(delidlist));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainFileQuestion().DeleteList(delidlist))
                {
                    resultMsg.Info = "删除成功";
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "删除失败";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }


        // PUT: api/T_UserInfo/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/T_UserInfo/5
        public void Delete(int id)
        {
        }
    }
}
